package com.cheng.im.service.group.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cheng.im.service.common.eunms.GroupErrorCode;
import com.cheng.im.service.common.eunms.GroupMemberRoleEnum;
import com.cheng.im.service.common.eunms.GroupStatusEnum;
import com.cheng.im.service.common.eunms.GroupTypeEnum;
import com.cheng.im.service.common.exception.ApplicationException;
import com.cheng.im.service.common.resp.CommonResult;
import com.cheng.im.service.group.domain.ImGroup;
import com.cheng.im.service.group.domain.ImGroupMember;
import com.cheng.im.service.group.mapper.ImGroupMemberMapper;
import com.cheng.im.service.group.model.req.*;
import com.cheng.im.service.group.model.resp.AddMemberResp;
import com.cheng.im.service.group.model.resp.GetRoleInGroupResp;
import com.cheng.im.service.group.service.ImGroupMemberService;
import com.cheng.im.service.group.service.ImGroupService;
import com.cheng.im.service.user.domain.ImUserData;
import com.cheng.im.service.user.service.ImUserDataService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
* @author Cheng
* @description 针对表【im_group_member】的数据库操作Service实现
* @createDate 2024-12-19 09:16:10
*/
@DubboService
public class ImGroupMemberServiceImpl extends ServiceImpl<ImGroupMemberMapper, ImGroupMember>
    implements ImGroupMemberService {

    @Resource
    private ImGroupService imGroupService;
    @Resource
    private ImGroupMemberService imGroupMemberService;
    @Resource
    private ImUserDataService imUserDataService;

    @Override
    public CommonResult importGroupMember(ImportGroupMemberReq req) {
        List<AddMemberResp> resp = new ArrayList<>();

        CommonResult<ImGroup> groupResp = imGroupService.getGroup(req.getGroupId(), req.getAppId());
        if (!groupResp.isOk()) {
            return groupResp;
        }

        for (GroupMemberDto memberId : req.getMembers()) {
            CommonResult responseVO = null;
            try {
                responseVO = imGroupMemberService.addGroupMember(req.getGroupId(), req.getAppId(), memberId);
            } catch (Exception e) {
                e.printStackTrace();
                responseVO = CommonResult.failed();
            }
            AddMemberResp addMemberResp = new AddMemberResp();
            addMemberResp.setMemberId(memberId.getMemberId());
            if (responseVO.isOk()) {
                addMemberResp.setResult(0);
            } else if (responseVO.getCode() == GroupErrorCode.USER_IS_JOINED_GROUP.getCode()) {
                addMemberResp.setResult(2);
            } else {
                addMemberResp.setResult(1);
            }
            resp.add(addMemberResp);
        }

        return CommonResult.success(resp);
    }

    /**
     * 添加群成员，拉人入群的逻辑，直接进入群聊。如果是后台管理员，则直接拉入群，
     * 否则只有私有群可以调用本接口，并且群成员也可以拉人入群.只有私有群可以调用本接口
     * @param req
     * @return
     */
    @Override
    public CommonResult addMember(AddGroupMemberReq req) {
        List<AddMemberResp> resp = new ArrayList<>();

        boolean isAdmin = false;
        CommonResult<ImGroup> groupResp = imGroupService.getGroup(req.getGroupId(), req.getAppId());
        if (!groupResp.isOk()) {
            return groupResp;
        }

        List<GroupMemberDto> memberDtos = req.getMembers();
//        if(appConfig.isAddGroupMemberBeforeCallback()){
//
//            ResponseVO responseVO = callbackService.beforeCallback(req.getAppId(), Constants.CallbackCommand.GroupMemberAddBefore
//                    , JSONObject.toJSONString(req));
//            if(!responseVO.isOk()){
//                return responseVO;
//            }
//
//            try {
//                memberDtos
//                        = JSONArray.parseArray(JSONObject.toJSONString(responseVO.getData()), GroupMemberDto.class);
//            }catch (Exception e){
//                e.printStackTrace();
//                log.error("GroupMemberAddBefore 回调失败：{}",req.getAppId());
//            }
//        }

        ImGroup group = groupResp.getData();


        /**
         * 私有群（private）	类似普通微信群，创建后仅支持已在群内的好友邀请加群，且无需被邀请方同意或群主审批
         * 公开群（Public）	类似 QQ 群，创建后群主可以指定群管理员，需要群主或管理员审批通过才能入群
         * 群类型 1私有群（类似微信） 2公开群(类似qq）
         *
         */

        if (!isAdmin && GroupTypeEnum.PUBLIC.getCode() == group.getGroupType()) {
            throw new ApplicationException(GroupErrorCode.THIS_OPERATE_NEED_APPMANAGER_ROLE);
        }

        List<String> successId = new ArrayList<>();
        for (GroupMemberDto memberId :
                memberDtos) {
            CommonResult responseVO = null;
            try {
                responseVO = imGroupMemberService.addGroupMember(req.getGroupId(), req.getAppId(), memberId);
            } catch (Exception e) {
                e.printStackTrace();
                responseVO = CommonResult.failed();
            }
            AddMemberResp addMemberResp = new AddMemberResp();
            addMemberResp.setMemberId(memberId.getMemberId());
            if (responseVO.isOk()) {
                successId.add(memberId.getMemberId());
                addMemberResp.setResult(0);
            } else if (responseVO.getCode() == GroupErrorCode.USER_IS_JOINED_GROUP.getCode()) {
                addMemberResp.setResult(2);
                addMemberResp.setResultMessage(responseVO.getMessage());
            } else {
                addMemberResp.setResult(1);
                addMemberResp.setResultMessage(responseVO.getMessage());
            }
            resp.add(addMemberResp);
        }

        // todo: 新增群成员消息
//        AddGroupMemberPack addGroupMemberPack = new AddGroupMemberPack();
//        addGroupMemberPack.setGroupId(req.getGroupId());
//        addGroupMemberPack.setMembers(successId);
//        groupMessageProducer.producer(req.getOperater(), GroupEventCommand.ADDED_MEMBER, addGroupMemberPack
//                , new ClientInfo(req.getAppId(), req.getClientType(), req.getImei()));
//
//        if(appConfig.isAddGroupMemberAfterCallback()){
//            AddMemberAfterCallback dto = new AddMemberAfterCallback();
//            dto.setGroupId(req.getGroupId());
//            dto.setGroupType(group.getGroupType());
//            dto.setMemberId(resp);
//            dto.setOperater(req.getOperater());
//            callbackService.callback(req.getAppId()
//                    ,Constants.CallbackCommand.GroupMemberAddAfter,
//                    JSONObject.toJSONString(dto));
//        }

        return CommonResult.success(resp);
    }

    @Override
    public CommonResult removeMember(RemoveGroupMemberReq req) {
        List<AddMemberResp> resp = new ArrayList<>();
        boolean isAdmin = false;
        CommonResult<ImGroup> groupResp = imGroupService.getGroup(req.getGroupId(), req.getAppId());
        if (!groupResp.isOk()) {
            return groupResp;
        }

        ImGroup group = groupResp.getData();

        if (!isAdmin) {
            if (GroupTypeEnum.PUBLIC.getCode() == group.getGroupType()) {

                //获取操作人的权限 是管理员or群主or群成员
                CommonResult<GetRoleInGroupResp> role = getRoleInGroupOne(req.getGroupId(), req.getOperator(), req.getAppId());
                if (!role.isOk()) {
                    return role;
                }

                GetRoleInGroupResp data = role.getData();
                Integer roleInfo = data.getRole();

                boolean isOwner = roleInfo == GroupMemberRoleEnum.OWNER.getCode();
                boolean isManager = roleInfo == GroupMemberRoleEnum.MAMAGER.getCode();

                if (!isOwner && !isManager) {
                    throw new ApplicationException(GroupErrorCode.THIS_OPERATE_NEED_MANAGER_ROLE);
                }

                //私有群必须是群主才能踢人
                if (!isOwner && GroupTypeEnum.PRIVATE.getCode() == group.getGroupType()) {
                    throw new ApplicationException(GroupErrorCode.THIS_OPERATE_NEED_OWNER_ROLE);
                }

                //公开群管理员和群主可踢人，但管理员只能踢普通群成员
                if (GroupTypeEnum.PUBLIC.getCode() == group.getGroupType()) {
//                    throw new ApplicationException(GroupErrorCode.THIS_OPERATE_NEED_MANAGER_ROLE);
                    //获取被踢人的权限
                    CommonResult<GetRoleInGroupResp> roleInGroupOne = this.getRoleInGroupOne(req.getGroupId(), req.getMemberId(), req.getAppId());
                    if (!roleInGroupOne.isOk()) {
                        return roleInGroupOne;
                    }
                    GetRoleInGroupResp memberRole = roleInGroupOne.getData();
                    if (memberRole.getRole() == GroupMemberRoleEnum.OWNER.getCode()) {
                        throw new ApplicationException(GroupErrorCode.GROUP_OWNER_IS_NOT_REMOVE);
                    }
                    //是管理员并且被踢人不是群成员，无法操作
                    if (isManager && memberRole.getRole() != GroupMemberRoleEnum.ORDINARY.getCode()) {
                        throw new ApplicationException(GroupErrorCode.THIS_OPERATE_NEED_OWNER_ROLE);
                    }
                }
            }
        }
        CommonResult responseVO = imGroupMemberService.removeGroupMember(req.getGroupId(), req.getAppId(), req.getMemberId());
        if(responseVO.isOk()){
            // todo: 移除群成员通知
//            RemoveGroupMemberPack removeGroupMemberPack = new RemoveGroupMemberPack();
//            removeGroupMemberPack.setGroupId(req.getGroupId());
//            removeGroupMemberPack.setMember(req.getMemberId());
//            groupMessageProducer.producer(req.getMemberId(), GroupEventCommand.DELETED_MEMBER, removeGroupMemberPack
//                    , new ClientInfo(req.getAppId(), req.getClientType(), req.getImei()));
//            if(appConfig.isDeleteGroupMemberAfterCallback()){
//                callbackService.callback(req.getAppId(),
//                        Constants.CallbackCommand.GroupMemberDeleteAfter,
//                        JSONObject.toJSONString(req));
//            }
        }

        return responseVO;
    }

    @Override
    public CommonResult addGroupMember(String groupId, String appId, GroupMemberDto dto) {
        CommonResult<ImUserData> singleUserInfo = imUserDataService.getSingleUserInfo(dto.getMemberId(), appId);
        if(!singleUserInfo.isOk()){
            return singleUserInfo;
        }

        if (dto.getRole() != null && GroupMemberRoleEnum.OWNER.getCode() == dto.getRole()) {
            LambdaQueryWrapper<ImGroupMember> queryOwner = new LambdaQueryWrapper<>();
            queryOwner.eq(ImGroupMember::getGroupId, groupId);
            queryOwner.eq(ImGroupMember::getAppId, appId);
            queryOwner.eq(ImGroupMember::getRole, GroupMemberRoleEnum.OWNER.getCode());
            Long ownerNum = baseMapper.selectCount(queryOwner);
            if (ownerNum > 0) {
                return CommonResult.failed(GroupErrorCode.GROUP_IS_HAVE_OWNER.getCode(),
                        GroupErrorCode.GROUP_IS_HAVE_OWNER.getError());
            }
        }

        LambdaQueryWrapper<ImGroupMember> query = new LambdaQueryWrapper<>();
        query.eq(ImGroupMember::getGroupId, groupId);
        query.eq(ImGroupMember::getAppId, appId);
        query.eq(ImGroupMember::getMemberId, dto.getMemberId());
        ImGroupMember memberDto = baseMapper.selectOne(query);

        if (memberDto == null) {
            //初次加群
            memberDto = new ImGroupMember();
            BeanUtils.copyProperties(dto, memberDto);
            memberDto.setGroupId(groupId);
            memberDto.setAppId(appId);
            memberDto.setJoinTime(System.currentTimeMillis());
            int insert = baseMapper.insert(memberDto);
            if (insert == 1) {
                return CommonResult.success();
            }
            return CommonResult.failed(GroupErrorCode.USER_JOIN_GROUP_ERROR.getCode(),
                    GroupErrorCode.USER_JOIN_GROUP_ERROR.getError());
        } else if (GroupMemberRoleEnum.LEAVE.getCode() == memberDto.getRole()) {
            //重新进群
            memberDto = new ImGroupMember();
            BeanUtils.copyProperties(dto, memberDto);
            memberDto.setJoinTime(System.currentTimeMillis());
            int update = baseMapper.update(memberDto, query);
            if (update == 1) {
                return CommonResult.success(null);
            }
            return CommonResult.failed(GroupErrorCode.USER_JOIN_GROUP_ERROR.getCode(),
                    GroupErrorCode.USER_JOIN_GROUP_ERROR.getError());
        }

        return CommonResult.failed(GroupErrorCode.USER_JOIN_GROUP_ERROR.getCode(),
                GroupErrorCode.USER_JOIN_GROUP_ERROR.getError());
    }

    @Override
    public CommonResult removeGroupMember(String groupId, String appId, String memberId) {
        CommonResult<ImUserData> singleUserInfo = imUserDataService.getSingleUserInfo(memberId, appId);
        if(!singleUserInfo.isOk()){
            return singleUserInfo;
        }

        CommonResult<GetRoleInGroupResp> roleInGroupOne = getRoleInGroupOne(groupId, memberId, appId);
        if (!roleInGroupOne.isOk()) {
            return roleInGroupOne;
        }

        GetRoleInGroupResp data = roleInGroupOne.getData();
        ImGroupMember imGroupMemberEntity = new ImGroupMember();
        imGroupMemberEntity.setRole(GroupMemberRoleEnum.LEAVE.getCode());
        imGroupMemberEntity.setLeaveTime(System.currentTimeMillis());
        imGroupMemberEntity.setGroupMemberId(data.getGroupMemberId());
        baseMapper.updateById(imGroupMemberEntity);
        return CommonResult.success();
    }

    @Override
    public CommonResult<GetRoleInGroupResp> getRoleInGroupOne(String groupId, String memberId, String appId) {
        GetRoleInGroupResp resp = new GetRoleInGroupResp();

        LambdaQueryWrapper<ImGroupMember> queryOwner = new LambdaQueryWrapper<>();
        queryOwner.eq(ImGroupMember::getGroupId, groupId);
        queryOwner.eq(ImGroupMember::getAppId, appId);
        queryOwner.eq(ImGroupMember::getMemberId, memberId);

        ImGroupMember imGroupMemberEntity = baseMapper.selectOne(queryOwner);
        if (imGroupMemberEntity == null || imGroupMemberEntity.getRole() == GroupMemberRoleEnum.LEAVE.getCode()) {
            return CommonResult.failed(GroupErrorCode.MEMBER_IS_NOT_JOINED_GROUP.getCode(),
                    GroupErrorCode.MEMBER_IS_NOT_JOINED_GROUP.getError());
        }

        resp.setSpeakDate(imGroupMemberEntity.getSpeakDate());
        resp.setGroupMemberId(imGroupMemberEntity.getGroupMemberId());
        resp.setMemberId(imGroupMemberEntity.getMemberId());
        resp.setRole(imGroupMemberEntity.getRole());
        return CommonResult.success(resp);
    }

    @Override
    public CommonResult<Collection<String>> getMemberJoinedGroup(GetJoinedGroupReq req) {
        if (req.getLimit() != null) {
            Page<ImGroupMember> objectPage = new Page<>(req.getOffset(), req.getLimit());
            LambdaQueryWrapper<ImGroupMember> query = new LambdaQueryWrapper<>();
            query.eq(ImGroupMember::getAppId, req.getAppId());
            query.eq(ImGroupMember::getMemberId, req.getMemberId());
            IPage<ImGroupMember> imGroupMemberEntityPage = baseMapper.selectPage(objectPage, query);

            Set<String> groupId = new HashSet<>();
            List<ImGroupMember> records = imGroupMemberEntityPage.getRecords();
            records.forEach(e -> {
                groupId.add(e.getGroupId());
            });

            return CommonResult.success(groupId);
        } else {
            return CommonResult.success(baseMapper.getJoinedGroupId(req.getAppId(), req.getMemberId()));
        }
    }

    @Override
    public CommonResult<List<GroupMemberDto>> getGroupMember(String groupId, String appId) {
        List<GroupMemberDto> groupMember = baseMapper.getGroupMember(appId, groupId);
        return CommonResult.success(groupMember);
    }

    /**
     * todo: 可以加缓存
     * @param groupId
     * @param appId
     * @return
     */
    @Override
    public List<String> getGroupMemberId(String groupId, String appId) {
        return baseMapper.getGroupMemberId(appId, groupId);
    }

    @Override
    public List<GroupMemberDto> getGroupManager(String groupId, String appId) {
        return baseMapper.getGroupManager(groupId, appId);
    }

    @Override
    public CommonResult updateGroupMember(UpdateGroupMemberReq req) {
        boolean isadmin = false;

        CommonResult<ImGroup> group = imGroupService.getGroup(req.getGroupId(), req.getAppId());
        if (!group.isOk()) {
            return group;
        }

        ImGroup groupData = group.getData();
        if (groupData.getStatus() == GroupStatusEnum.DESTROY.getCode()) {
            throw new ApplicationException(GroupErrorCode.GROUP_IS_DESTROY);
        }

        //是否是自己修改自己的资料
        boolean isMeOperate = req.getOperator().equals(req.getMemberId());

        if (!isadmin) {
            //昵称只能自己修改 权限只能群主或管理员修改
            if (StringUtils.isBlank(req.getAlias()) && !isMeOperate) {
                return CommonResult.failed(GroupErrorCode.THIS_OPERATE_NEED_ONESELF.getCode(),
                        GroupErrorCode.THIS_OPERATE_NEED_ONESELF.getError());
            }
            //私有群不能设置管理员
            if (groupData.getGroupType() == GroupTypeEnum.PRIVATE.getCode() &&
                    req.getRole() != null && (req.getRole() == GroupMemberRoleEnum.MAMAGER.getCode() ||
                    req.getRole() == GroupMemberRoleEnum.OWNER.getCode())) {
                return CommonResult.failed(GroupErrorCode.THIS_OPERATE_NEED_MANAGER_ROLE.getCode(),
                        GroupErrorCode.THIS_OPERATE_NEED_MANAGER_ROLE.getError());
            }

            //如果要修改权限相关的则走下面的逻辑
            if(req.getRole() != null){
                //获取被操作人的是否在群内
                CommonResult<GetRoleInGroupResp> roleInGroupOne = this.getRoleInGroupOne(req.getGroupId(), req.getMemberId(), req.getAppId());
                if(!roleInGroupOne.isOk()){
                    return roleInGroupOne;
                }

                //获取操作人权限
                CommonResult<GetRoleInGroupResp> operateRoleInGroupOne = this.getRoleInGroupOne(req.getGroupId(), req.getOperator(), req.getAppId());
                if(!operateRoleInGroupOne.isOk()){
                    return operateRoleInGroupOne;
                }

                GetRoleInGroupResp data = operateRoleInGroupOne.getData();
                Integer roleInfo = data.getRole();
                boolean isOwner = roleInfo == GroupMemberRoleEnum.OWNER.getCode();
                boolean isManager = roleInfo == GroupMemberRoleEnum.MAMAGER.getCode();

                //不是管理员不能修改权限
                if(req.getRole() != null && !isOwner && !isManager){
                    return CommonResult.failed(GroupErrorCode.THIS_OPERATE_NEED_MANAGER_ROLE.getCode(),
                            GroupErrorCode.THIS_OPERATE_NEED_MANAGER_ROLE.getError());
                }

                //管理员只有群主能够设置
                if(req.getRole() != null && req.getRole() == GroupMemberRoleEnum.MAMAGER.getCode() && !isOwner){
                    return CommonResult.failed(GroupErrorCode.THIS_OPERATE_NEED_OWNER_ROLE.getCode(),
                            GroupErrorCode.THIS_OPERATE_NEED_OWNER_ROLE.getError());
                }

            }
        }

        ImGroupMember update = new ImGroupMember();

        if (StringUtils.isNotBlank(req.getAlias())) {
            update.setAlias(req.getAlias());
        }

        //不能直接修改为群主
        if(req.getRole() != null && req.getRole() != GroupMemberRoleEnum.OWNER.getCode()){
            update.setRole(req.getRole());
        }

        UpdateWrapper<ImGroupMember> objectUpdateWrapper = new UpdateWrapper<>();
        objectUpdateWrapper.eq("app_id", req.getAppId());
        objectUpdateWrapper.eq("member_id", req.getMemberId());
        objectUpdateWrapper.eq("group_id", req.getGroupId());
        baseMapper.update(update, objectUpdateWrapper);

        // todo: 群成员变更通知
//        UpdateGroupMemberPack pack = new UpdateGroupMemberPack();
//        BeanUtils.copyProperties(req, pack);
//        groupMessageProducer.producer(req.getOperater(), GroupEventCommand.UPDATED_MEMBER, pack, new ClientInfo(req.getAppId(), req.getClientType(), req.getImei()));


        return CommonResult.success();
    }

    @Override
    @Transactional
    public CommonResult transferGroupMember(String owner, String groupId, String appId) {
        //更新旧群主
        ImGroupMember imGroupMemberEntity = new ImGroupMember();
        imGroupMemberEntity.setRole(GroupMemberRoleEnum.ORDINARY.getCode());
        UpdateWrapper<ImGroupMember> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("app_id", appId);
        updateWrapper.eq("group_id", groupId);
        updateWrapper.eq("role", GroupMemberRoleEnum.OWNER.getCode());
        baseMapper.update(imGroupMemberEntity, updateWrapper);

        //更新新群主
        ImGroupMember newOwner = new ImGroupMember();
        newOwner.setRole(GroupMemberRoleEnum.OWNER.getCode());
        UpdateWrapper<ImGroupMember> ownerWrapper = new UpdateWrapper<>();
        ownerWrapper.eq("app_id", appId);
        ownerWrapper.eq("group_id", groupId);
        ownerWrapper.eq("member_id", owner);
        baseMapper.update(newOwner, ownerWrapper);

        return CommonResult.success();
    }

    @Override
    public CommonResult speak(SpeaMemberReq req) {
        CommonResult<ImGroup> groupResp = imGroupService.getGroup(req.getGroupId(), req.getAppId());
        if (!groupResp.isOk()) {
            return groupResp;
        }

        boolean isadmin = false;
        boolean isOwner = false;
        boolean isManager = false;
        GetRoleInGroupResp memberRole = null;

        if (!isadmin) {

            //获取操作人的权限 是管理员or群主or群成员
            CommonResult<GetRoleInGroupResp> role = getRoleInGroupOne(req.getGroupId(), req.getOperator(), req.getAppId());
            if (!role.isOk()) {
                return role;
            }

            GetRoleInGroupResp data = role.getData();
            Integer roleInfo = data.getRole();

            isOwner = roleInfo == GroupMemberRoleEnum.OWNER.getCode();
            isManager = roleInfo == GroupMemberRoleEnum.MAMAGER.getCode();

            if (!isOwner && !isManager) {
                throw new ApplicationException(GroupErrorCode.THIS_OPERATE_NEED_MANAGER_ROLE);
            }

            //获取被操作的权限
            CommonResult<GetRoleInGroupResp> roleInGroupOne = this.getRoleInGroupOne(req.getGroupId(), req.getMemberId(), req.getAppId());
            if (!roleInGroupOne.isOk()) {
                return roleInGroupOne;
            }
            memberRole = roleInGroupOne.getData();
            //被操作人是群主只能app管理员操作
            if (memberRole.getRole() == GroupMemberRoleEnum.OWNER.getCode()) {
                throw new ApplicationException(GroupErrorCode.THIS_OPERATE_NEED_APPMANAGER_ROLE);
            }

            //是管理员并且被操作人不是群成员，无法操作
            if (isManager && memberRole.getRole() != GroupMemberRoleEnum.ORDINARY.getCode()) {
                throw new ApplicationException(GroupErrorCode.THIS_OPERATE_NEED_OWNER_ROLE);
            }
        }

        ImGroupMember imGroupMemberEntity = new ImGroupMember();
        if(memberRole == null){
            //获取被操作的权限
            CommonResult<GetRoleInGroupResp> roleInGroupOne = this.getRoleInGroupOne(req.getGroupId(), req.getMemberId(), req.getAppId());
            if (!roleInGroupOne.isOk()) {
                return roleInGroupOne;
            }
            memberRole = roleInGroupOne.getData();
        }

        imGroupMemberEntity.setGroupMemberId(memberRole.getGroupMemberId());
        if(req.getSpeakDate() > 0){
            imGroupMemberEntity.setSpeakDate(System.currentTimeMillis() + req.getSpeakDate());
        }else{
            imGroupMemberEntity.setSpeakDate(req.getSpeakDate());
        }

        int i = baseMapper.updateById(imGroupMemberEntity);
        // todo: 禁言通知
//        if(i == 1){
//            GroupMemberSpeakPack pack = new GroupMemberSpeakPack();
//            BeanUtils.copyProperties(req,pack);
//            groupMessageProducer.producer(req.getOperater(),GroupEventCommand.SPEAK_GOUP_MEMBER,pack,
//                    new ClientInfo(req.getAppId(),req.getClientType(),req.getImei()));
//        }
        return CommonResult.success();
    }

    @Override
    public CommonResult<Collection<String>> syncMemberJoinedGroup(String operater, String appId) {
        return CommonResult.success(baseMapper.syncJoinedGroupId(appId,operater,GroupMemberRoleEnum.LEAVE.getCode()));
    }
}




